<?php

namespace App\Controller\Admin;

use App\Middleware\AdminMiddleware;
use App\Model\QmGameSpecial;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;

#[Controller(prefix: '/admin/Special'), Middleware(AdminMiddleware::class)]
class SpecialController extends BaseController {

    /**
     * 获取人员列表
     * @return array
     */
    #[GetMapping(path: 'index')]
    public function index(): array {
        $limit = $this->request->query('size', config('apiadmin.admin_list_default'));
        $game_hash = $this->request->query('game_hash', '3a86e68287aae7ae3d2c19ce64af2823');

        $obj = QmGameSpecial::query();
        $obj = $obj->where('game_hash', $game_hash);
        $listObj = $obj->orderByDesc('id')->paginate((int)$limit);

        return $this->buildSuccess([
            'list'  => $listObj->items(),
            'count' => $listObj->total()
        ]);
    }

    /**
     * 新增人员
     * @return array
     */
    #[PostMapping(path: 'add')]
    public function add(): array {
        if ($this->request->post('openid')) {
            $obj = new QmGameSpecial();
            $obj->game_hash = $this->request->post('game_hash', '3a86e68287aae7ae3d2c19ce64af2823');
            $obj->openid = $this->request->post('openid');
            $obj->save();
        }
        return $this->buildSuccess();
    }

    /**
     * 删除人员
     * @return array
     */
    #[GetMapping(path: 'del')]
    public function del(): array {
        if ($this->request->query('id')) {
            QmGameSpecial::query()->where('id', $this->request->query('id'))->delete();
        }
        return $this->buildSuccess();
    }

}